package com.jie.listening;

import com.jie.entity.Article;
import com.jie.enums.ArticleFlowEvent;
import com.jie.po.ArticleAuditPO;
import com.jie.po.EmailCodePO;
import com.jie.po.req.ArticleReq;
import com.jie.service.IArticleService;
import com.jie.state.ArticleStateServiceImpl;
import com.jie.state.StateConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import static com.jie.constants.RabbitMQConstant.*;

/**
 * @Author: 杰
 * @Project :net-disk
 * @DateTime: 2024/5/30 12:56
 **/
@Component
@Slf4j
public class ArticleListenConsumer {
    @Autowired
    private IArticleService articleService;

    @Autowired
    private ArticleStateServiceImpl articleStateService;
    /**
     * 监听文章审核和保存
     *
     * @param article
     */
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(value = ARTICLE_PUBLISH_QUEUE, durable = "true", autoDelete = "false"),
                    exchange = @Exchange(value = ARTICLE_PUBLISH_EXCHANGE),
                    key = ARTICLE_PUBLISH_KEY
            )}, containerFactory = "rabbitListenerContainerFactory")
    public void listenArticle(@Payload Article article) {
        //审核文章
        boolean b = articleService.aigcApprovedArticleInfo(article);
        //添加审核结果
        if (b) {
            //审核通过，进行发布
            articleStateService.sendEvent(ArticleFlowEvent.PUBLISH, article, StateConstants.PUBLISH);
        }
        log.info("通知前端{}",article);
        //这里应该发送给IM的websocket对前端进行通知
//        sendSystematicNotificationArticle(articleReq);
    }
}
